home *** CD-ROM | disk | FTP | other *** search
- Path: cs.ruu.nl!usenet
- From: wsldanke@cs.ruu.nl (Wessel Dankers)
- Newsgroups: comp.sys.amiga.programmer
- Subject: x ^= y ^= x ^= y;
- Date: 20 Feb 96 23:59:04 +0100
- Organization: Dept of Computer Science, Utrecht University, The Netherlands
- Message-ID: <1286.6624T1439T237@cs.ruu.nl>
- NNTP-Posting-Host: anx1p8.cc.ruu.nl
- X-Newsreader: THOR 2.22 (Amiga TCP/IP)
-
- In one of my college-books I found this transition:
-
- <x := x+y ; y := x-y ; x := x-y, s> -> s{s(y)/x}{s(x)/y}
-
- proven correct.
-
- In plain C:
-
- x = x + y;
- y = x - y;
- x = x - y;
-
- will swap the integers x and y. Of course I immediately started `hacking'
- on it, resulting in:
-
- x = x ^ y;
- y = x ^ y;
- x = x ^ y;
-
- with the C bitwise-or operator.
-
- Which can of course be rewritten as:
-
- x ^= y;
- y ^= x;
- x ^= y;
-
- or if you want it real fancy:
-
- x ^= y ^= x ^= y;
-
- Note that these algorithms do not use temporary storage and can be
- generalized for large memory areas with a for-loop.
-
- My questions are:
-
- a) are these routines efficient for integers? (probably not)
-
- b) is this x ^= y ^= x ^= y; method usable with the blitter?
- It would be possible to swap two memory locations with no
- extra memory required in three Blit calls. Is the blitter
- equally fast with copying as with XOR'ing?
-
- --
- Wessel Dankers _\\|//_ <wsldanke@cs.ruu.nl>
- ///|\\\
- ----------------------------oOO--(_)---OOo----------------------------
- `Never imagine yourself not to be otherwise than what it might appear
- to others that what you were or might have been was not otherwise than
- what you had been would have appeared to them to be otherwise.'
-
-